package com.hgj.web;

import com.alibaba.fastjson.JSON;
import com.hgj.dao.UserDao;
import com.hgj.dao.impl.UserDaoImpl;
import com.hgj.entity.JSONBean;
import com.hgj.entity.User;
import com.hgj.util.MD5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user/*")
public class UserServlet extends HttpServlet {
   UserDao userDao=new UserDaoImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //获取项目基准路径
        String baseURL = request.getContextPath();
        //截取项目二级路径
        String uri = request.getRequestURI();
        System.out.println("uri = " + uri);
        JSONBean jsonBean = new JSONBean();
        String path = uri.substring(uri.lastIndexOf("/") + 1);
        System.out.println("path = " + path);
        if ("login.do".equals(path)) {  //登录
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            System.out.println("password = " + password);
            User user = userDao.login(name);
            if (null == user) {
                jsonBean.setFailed("用户不存在");
            } else {//如果用户名存在，则判断密码是否正确
//                    先对密码进行加密，如果秘密正确则继续执行代码
                password=MD5Util.MD5(password);
                if (!password.equals(user.getUser_password())) {
                    jsonBean.setFailed("密码错误");
                } else {
                    jsonBean.setData(user);
                    jsonBean.setSuccess("登录成功");
                }
            }
            //返回json格式
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            String json = JSON.toJSONString(jsonBean);
            out.print(json);
            out.close();

        } else if ("check.do".equals(path)) {
            String key = request.getParameter("key");
            String val = request.getParameter("val");
            boolean is = userDao.check(key, val);
            String user_name=request.getParameter("user_name");
            String user_phone=request.getParameter("user_phone");
            String user_password=request.getParameter("user_password");
            User user=new User();
            user.setUser_name(user_name);
            user.setUser_phone(user_phone);
            user.setUser_password(user_password);
            int save=userDao.save(user);
            if (is) {
                jsonBean.setFailed("用户已存在，请重新输入");
            } else {
                jsonBean.setSuccess("注册成功");

            }
            response.setContentType("application/json;charset=utf-8");
            String result = JSON.toJSONString(jsonBean);
            PrintWriter writer = response.getWriter();
            writer.print(result);
            writer.close();
        } else {
            throw new RuntimeException("请求路径不存在");
        }
    }




}
